<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Article extends CI_Controller {

	public function index($start = 0){
	   $this->load->model('Articles', null, true);
       
       $this->load->library('pagination');
       $cofigPagination = $this->config->item('pagination');

        $config['base_url'] = base_url().'article/index';
        $config['total_rows'] = $this->Articles->getCount();
        $config['per_page'] = $cofigPagination['number_row']; 
        $this->pagination->initialize($config); 

        
       $data['Articles'] = $this->Articles->getList($start, $cofigPagination['number_row']);
	   $this->load->view('article/index', $data);
	}
    
	public function saveordering(){
		$this->load->model('Articles', null, true);
		$data = $this->input->post();
		$this->Articles->saveOrdering($data);
		redirect(base_url().'article.html');
	} 
    /**
     * function add customer
     * @author tien.nguyen
     */
    public function add(){
        $data = array();
        $this->load->model('Articles', null, true);
        $this->load->model('ArticleCategories', null, true);
        
        //get post
        if($this->input->post()){
            $data = $this->input->post();
            //upload images
            if($_FILES['Image']['name'] != ""){
                $resUpload = $this->_uploadFile('Image', $data);
                if(!is_null($resUpload)){       
                    $data['Image'] = $resUpload['file_name'];
                    if($this->Articles->add($data)){
                        redirect(base_url().'article.html');
                    }
                }
            }else{
                $data['Image'] = '';
                if($this->Articles->add($data)){
                        redirect(base_url().'article.html');
                }
            }
            $data['Article'] = $data;
        }
        $data['OptionArtCate'] = $this->ArticleCategories->getOptions(); 
        $this->load->view('article/add', $data);
    }
    
    /**
     * Delete customer
     * @author tien.nguyen
     */
    public function delete($articleId){
         $this->load->model('Articles', null, true);
         if($this->Articles->delete($articleId)){
            
            redirect(base_url().'article.html');
         }
    }
    
    /**
     * edit save config id
     * @author tien.nguyen
     */
    public function edit($articleId){
        $res = false;
        $this->load->model('Articles', null, true);
        $this->load->model('ArticleCategories', null, true);
        
        if($this->input->post()){
             $data = $this->input->post();             
             if($_FILES['Image']['name'] != ''){
                $resUpload = $this->_uploadFile('Image');
                if(!is_null($resUpload)){ 
                    $data['Image'] = $resUpload['file_name'];
                    if($this->Articles->edit($data, $articleId)){
                        redirect(base_url().'article.html');
                    }
                }          
             }else{
                if($this->Articles->edit($data, $articleId)){
                    redirect(base_url().'article.html');
                }
             }    
        }
        
        $data['Article'] = $this->Articles->getArticle($articleId);
        $data['OptionArtCate'] = $this->ArticleCategories->getOptions(); 
        $this->load->view('article/edit', $data);       
    }
    
    /**
     * upload file 
     * @author tien.nguyen
     */
    private function _uploadFile($fileName, &$data){
       $config['upload_path'] = $this->config->item('base_upload').'/news';;
       $config['allowed_types'] = 'gif|png|jpg';
       $config['max_size'] = '1000';
       $config['max_width'] =  '3000';
       $config['max_height'] = '3000';
       
       $this->load->library('upload', $config);
       if(!$this->upload->do_upload($fileName)){
            $data['message_error'] = $this->upload->error_msg;
            return null;
       }else{
            return $this->upload->data();
       }
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */